<template>
  <div class="userlogin">
        <img src="./images/返回.png" alt="" class="back" @click="backTo">
        <h2>用户注册</h2>
        <div class="userImgPart">
            <div class="userImg">
                <img src="./images/123.png" alt="">
            </div>
        </div>
        <form action="" class="loginform">
            <label class="label">手机</label>
            <input type="text" placeholder="请输入手机号" v-model="form.phoneNumber" /><br>
            <label class="label input-jianju">账号</label>
            <input class="input-jianju" type="text" placeholder="请输入手机号" v-model="form.userName" /><br>
            <label class="label input-jianju">密码</label>
            <input class="input-jianju" type="password" placeholder="请输入密码" v-model="form.password" /><br>
            <label class="label input-jianju queren">密码</label>
            <input class="input-jianju" type="password" placeholder="请输入确认密码" v-model="form.confirmPwd" />
            <button class="register" @click="register.prevent" >注册</button>
        </form>
    </div>
</template>

<script>
import {getRegister} from '../../api/index';
export default {
    name:'UserRegister',
    data(){
        return{
            form:{
                phoneNumber:'',
                userName:'',
                password:'',
                confirmPwd:'',
            }
        }
    },
    methods:{
        backTo(){
            this.$router.push('/userLogin');
        },
        async register(){
            if(this.form.password!==this.form.confirmPwd){
                alert('请输入相同的密码！');
            }else{
                /*
                @return body
                {
                    "success": true,
                    "message": "注册成功",
                    "code": 200,
                    "result": null,
                    "timestamp": 1664153936317
                }*/

                const result = await getRegister({phone:this.form.phoneNumber,username:this.form.userName,password:this.form.password});
                if(!result.success){
                    // console.log(result);
                    alert('已存在该用户请重新注册');
                    this.reset.call(this);
                }else{
                    alert('注册成功');
                    this.$store.commit('UserLogin/STOREUSERREGISTER',this.form);
                    this.$router.push('/userLogin');
                }
            }
        },
        reset(){
            this.form = this.$options.data().form;
        }
    }

}
</script>

<style scoped>
    * {
        margin: 0;
        padding: 0;
        height: 100%;
    }
    
    .userlogin {
        width: 95%;
        height: 90%;
        position: absolute;
        top: 5%;
        left: 2.5%;
    }
    
    div h2 {
        color: slategray;
        display: inline;
        margin-left: 30px;
    }
    
    .userImgPart {
        position: relative;
        height: 150px;
        width: 100%;
    }
    
    .userImg {
        position: relative;
        top: 37.5px;
        height: 75px;
        width: 75px;
        margin: auto;
        border-radius: 37.5px 37.5px;
        border: solid 1px gainsboro;
        overflow: hidden;
    }
    
    .loginInfo {
        margin: 0;
        height: 500px;
    }
    
    .loginform {
        height: 500px;
        display: block;
        font-size: 20px;
        overflow: hidden;
        position: relative;
    }
    
    .label {
        font-size: 20px;
        font-weight: 400;
        margin-left: 3px;
        position: absolute;
        height: 35px;
        line-height: 35px;
    }
    
    input {
        position: relative;
        width: 95%;
        height: 35px;
        font-size: 20px;
        background: transparent;
        text-indent: 50px;
        border: solid 2px skyblue;
        line-height: 35px;
    }
    
    .login {
        position: absolute;
        width: 100%;
        height: 50px;
        bottom: 55px;
        left: 0;
        text-align: center;
        background-color: darkgray;
        border-radius: 5px;
        border: none;
    }
    
    .register {
        position: absolute;
        width: 100%;
        height: 50px;
        bottom: 0px;
        left: 0;
        text-align: center;
        background-color: skyblue;
        border-radius: 5px;
        border: none;
        color: white;
    }
    
    .input-jianju {
        margin-top: 35px;
    }
    
    .back {
        height: 30px;
        width: 30px;
        position: absolute;
        margin-top: 2px;
    }
</style>